package controller

import (
	
	"reflect"
	"strings"
	

	"github.com/gin-gonic/gin"
	"github.com/golang-module/carbon/v2"
)

type BaseController struct{}

func (con BaseController) Success(c *gin.Context, data interface{}, msg string, status int) {
	dataMap := make(map[string]any)
	if len(msg) == 0 {
		msg = "مەشغۇلات ئوڭۇشلۇق بولدى"
	}
	dataMap["status"] = status
	dataMap["success"] = true
	dataMap["data"] = data
	dataMap["msg"] = msg
	dataMap["time"] = carbon.Now().Format("Y-m-d H:i:s")

	c.JSON(status, dataMap)

}
func (con BaseController) Fail(c *gin.Context, msg string) {

	c.JSON(500., gin.H{
		"success": false,
		"status":  500,
		"msg":     msg,
		"time":    carbon.Now().Format("Y-m-d H:i:s"),
	})
}

func (con BaseController) SuccessRaw(c *gin.Context, data map[string]interface{}, msg string, status int) {
	dataMap := make(map[string]any)

	dataMap["status"] = status

	dataMap["msg"] = msg
	dataMap["time"] = carbon.Now().Format("Y-m-d H:i:s")

	for k, v := range data {
		dataMap[k] = v
	}

	c.JSON(status, dataMap)

}

func Struct2Map(obj interface{}) map[string]interface{} {
	t := reflect.TypeOf(obj)
	v := reflect.ValueOf(obj)

	var data = make(map[string]interface{})
	for i := 0; i < t.NumField(); i++ {
		data[strings.ToLower(t.Field(i).Name)] = v.Field(i).Interface()
	}
	return data
}

func (con BaseController) Trans(c *gin.Context, key string) (result string) {

	return key

}

func (con BaseController) IsIPhone(c *gin.Context) bool {
	ua := strings.ToLower(c.Request.Header.Get("User-Agent"))
	if strings.Contains(ua, "iphone") || strings.Contains(ua, "ipad") {
		return true
	}
	return false

}



